package com.iSnail.ms.gather.ui.table;

import java.awt.Component;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableRowSorter;

import com.iSnail.ms.gather.dao.GatherDataDao;
import com.iSnail.ms.gather.entity.GatherRecord;
import com.iSnail.ms.util.AppUtil;
import com.iSnail.ms.util.Const;
import com.iSnail.ms.util.I18N;

public class GatherTable {
	String[] header = {I18N.getString(Const.TEMPERATURE),
						I18N.getString(Const.STRAIN),I18N.getString(Const.STRESS),
						I18N.getString(Const.AUSFORM),I18N.getString(Const.RECORD_TIME)};
	String[][] data = null;
	JTable table = new JTable(){
		public boolean isCellEditable(int row, int column) {
			return false;
		}
		public TableCellRenderer getCellRenderer(int row, int column) {
            TableCellRenderer renderer = super.getCellRenderer(row, column);
            if (renderer instanceof JLabel) {
                ((JLabel) renderer).setHorizontalAlignment(JLabel.CENTER);
            }
            return renderer;
        }
	};
	DefaultTableModel model = new DefaultTableModel();
	JScrollPane panel = null;

	public GatherTable(){
		initTable();
		panel = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	}
	
	private void initTable(){
		DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer();
		renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);

	}

	public void generateData(int id) {
		GatherDataDao dao = new GatherDataDao();
		try {
			ArrayList<GatherRecord> records = dao.getRecordByID(id);
			int recordsize = records.size();
			data = new String[recordsize][5];
			GatherRecord record = null;
			for(int index=0;index<recordsize;index++){
				record = records.get(index);
//				data[index][0]=String.valueOf(record.getTemperature());
//				data[index][1]=String.valueOf(record.getStrain());
//				data[index][2]=String.valueOf(record.getStress());
//				data[index][3]=String.valueOf(record.getAusform());
//				data[index][4]=AppUtil.getTimeDesc(record.getTime().getTime());									
			}
			TableRowSorter sorter = new TableRowSorter(model);
			model.setDataVector(data, header);
			table.setModel(model);
			table.setRowSorter(sorter);
		} catch (Exception e1) {
			e1.printStackTrace();
		}		
	}

	public Component getTable(){
		return panel;
	}

}